package com.java.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.java.model.*;
import com.java.service.*;
import com.java.utils.CommonUtils;
import com.java.utils.JsonBody;
import com.java.utils.ResponseUtil;;

@RestController
@RequestMapping("/pinglun")
public class PingLunController {
	private PingLunService pinglunService;

	public PingLunService getPingLunService() {
		return pinglunService;
	}

	@Autowired
	public void setPingLunService(PingLunService pinglunService) {
		this.pinglunService = pinglunService;
	}

	@Autowired
	private ThingService thingService;
	@Autowired
	private ClientService clientService;

	@Autowired
	private HttpServletRequest request;
	@Autowired
	private HttpServletResponse response;


	@CrossOrigin(origins = "*")
	@ResponseBody
	@RequestMapping("add")
	public JsonBody add(@RequestBody PingLun jp) {
		if(jp.getClinetId()==null)
			return Util.SetMap("您尚未登陆", false);
		if( pinglunService.Add(jp)>0)
			return Util.SetMap("点评成功", true);
		else
			return Util.SetMap("点 评失败", false);
	}

	@CrossOrigin(origins = "*")
	@ResponseBody
	@RequestMapping("list")
	public JsonBody list(int clientId) {

			List<PingLun> list = pinglunService.GetByClientID(clientId);
			for(int i=0;i<list.size();i++)
			{
				Thing j=thingService.GetByID(list.get(i).getThingId());
				list.get(i).setThing(j);;
			}
			return Util.SetData(list);
	}


	@CrossOrigin(origins = "*")
	@ResponseBody
	@RequestMapping("del")
	public JsonBody Del(int id) {
		if(pinglunService.Del(id)>0)
			return Util.SetMap("删除 成功", true);
		else
			return Util.SetMap("删除失败", false);
	}


}
